package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.file.AvatarByBase64Dto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.FileService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/file")
public class FileController {
    private final FileService fileService;
    private final HttpServletResponse response;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/download")
    @ResponseStatus(HttpStatus.OK)
    public void download() throws IOException {
        String filename = "book.csv";

        // Sets the response headers.
        response.setContentType("application/csv");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);

        // Writes content to response output stream.
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            outputStream.write("This is testing".getBytes());
            outputStream.flush();
        }
    }

    @PostMapping("/upload-own-avatar-by-base64")
    public ResponseEntity<ApiResponse<String>> uploadOwnAvatarByBase64(@Valid @RequestBody AvatarByBase64Dto data) {
        // Gets current user's id to update the avatar info.
        String userId = requestIdentityExtractor.getClaims().id;

        // Uploads the avatar file and updates the user avatar info.
        String avatarUrl = fileService.uploadAvatarByBase64(userId, data.getFilename(), data.getBase64Content());

        // Sends the avatar url back to client.
        return httpResponseHelper.getSuccessResponse(avatarUrl);
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("files") List<MultipartFile> files) {
        for (MultipartFile file : files) {
            System.out.println("Uploading file " + file.getOriginalFilename());
        }

        //            // Ensure directory exists
        //            File uploadDir = new File("D:\\Temp\\family-expense-resources");
        //
        //            if (!uploadDir.exists()) {
        //                uploadDir.mkdirs();
        //            }
        //
        //            // Save file to the server
        //            File serverFile = new File("UPLOAD_DIR" + file.getOriginalFilename());

        return ResponseEntity.ok("File uploaded successfully: ");
    }
}
